package aomo.mmma

import akka.actor.typed.{Behavior, SupervisorStrategy}
import akka.actor.typed.scaladsl.{Behaviors, Routers}

/**
 *
 * @author liuzhenchuan@foxmail.com
 * @date 2021/3/26
 */
object Template {

  object Worker {
    sealed trait Command
    case class DoLog(text: String) extends Command

    def apply(): Behavior[Command] = Behaviors.setup { context =>
      context.log.info("Starting worker")

      Behaviors.receiveMessage {
        case DoLog(text) =>
          context.log.info("Got message {}", text)
          Behaviors.same
      }
    }
  }

  Behaviors.setup[Unit] { ctx =>
    val pool = Routers.pool(poolSize = 4) {
      // make sure the workers are restarted if they fail
      Behaviors.supervise(Worker()).onFailure[Exception](SupervisorStrategy.restart)
    }.withRoundRobinRouting()
    val router = ctx.spawn(pool, "node-dispatcher")
    Behaviors.same
  }

}
